package imse.team1.invoicepayment.ws;

import imse.team1.invoicepayment.data.*;
import imse.team1.invoicepayment.ws.imse.team1.invoicepayment.utils.CachedInvoices;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.lang.InterruptedException;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Evgenia
 * Date: 6/21/12
 * Time: 11:24 AM
 * To change this template use File | Settings | File Templates.
 */
public class InvoicePaymentServiceSkeleton implements InvoicePaymentServiceSkeletonInterface {

    public InvoiceStatus placeInvoice(Invoice invoice) {
        String invoiceID = invoice.getInvoiceID().getInvoiceID();
        Double invoicePaymentAmmount = invoice.getPaymentAmmount().getPaymentAmmount();
        InvoiceStatus currentInvoiceStatus = new InvoiceStatus();
        currentInvoiceStatus.setStatus(StatusEnum.PLACED);
        InvoiceID nInvoiceId = new InvoiceID();
        nInvoiceId.setInvoiceID(invoiceID);
        currentInvoiceStatus.setInvoiceID(nInvoiceId);
        CachedInvoices.placedInvoices.put(invoiceID, currentInvoiceStatus);

        return currentInvoiceStatus;
    }


    public InvoiceStatus checkInvoiceStatus(InvoiceID invoiceID) {
        String invoiceId = invoiceID.getInvoiceID();
        InvoiceStatus invoiceSt = new InvoiceStatus();
        InvoiceID invId = new InvoiceID();
        invId.setInvoiceID(invoiceID.getInvoiceID());
        invoiceSt.setInvoiceID(invId);
        synchronized (CachedInvoices.placedInvoices) {
            Iterator it = CachedInvoices.placedInvoices.entrySet().iterator();
            boolean invoiceFound = false;
            while (it.hasNext()) {
                Map.Entry pairs = (Map.Entry) it.next();
                String invoiceID1 = (String) pairs.getKey();

                if (invoiceId.equals(invoiceID1)) {
                    int index = invoiceID1.indexOf("-");
                    if (index > -1) {
                        String clientID = invoiceId.substring(0, index);
                        if (clientID.startsWith("13")) {
                            InvoiceStatus invoiceStatus = new InvoiceStatus();
                            invoiceStatus.setStatus(StatusEnum.NOTPAID);
                            pairs.setValue(invoiceStatus);
                            invoiceSt.setStatus(StatusEnum.NOTPAID);
                        } else {
                            InvoiceStatus invoiceStatus = new InvoiceStatus();
                            invoiceStatus.setStatus(StatusEnum.PAID);
                            pairs.setValue(invoiceStatus);
                            invoiceSt.setStatus(StatusEnum.PAID);
                        }
                    } else {
                        InvoiceStatus invoiceStatus = new InvoiceStatus();
                        invoiceStatus.setStatus(StatusEnum.PAID);
                        pairs.setValue(invoiceStatus);
                        invoiceSt.setStatus(StatusEnum.PAID);
                    }
                    invoiceFound = true;
                }
            }
            if (!invoiceFound) {
                invoiceSt.setStatus(StatusEnum.NOTPAID);
            }
        }
        return invoiceSt;
    }


}
